package com.ys.controller.api.business;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.entity.business.TsOrganizationAccount;
import com.ys.service.business.TsOrganizationAccountService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tsOrganizationAccount")
public class ApiInterTsOrganizationAccountController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTsOrganizationAccountController.class);

    @Autowired
    private CommonApiService<TsOrganizationAccount> commonApiService;

    @Autowired
    private TsOrganizationAccountService tsOrganizationAccountService;

    @RequiresPermissions("tsOrganizationAccount:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TsOrganizationAccount cond,String conditions){
        //查询条件
        String nameLinked = ConditionUtil.getConditionValue(conditions, "userName");//工号/姓名
        if (nameLinked != null){
            cond.where("B.UserId LIKE '%" + nameLinked + "%'" + " OR  B.UserName LIKE '%" + nameLinked + "%'");
        }
        cond.addField("B.VGsJc  AS vgsjc");
        cond.addField("C.OrgName  AS deptName");
        cond.addField("D.RoleName  AS roleName");
        cond.addField("(D.UserId+ '/' +D.UserName)  AS userName");
        cond.addLeftJoin("THR_OperateCompanyApply B on B.VGsBm = A.Vgsbm");
        cond.addLeftJoin("TS_OrganizationInfo C on C.Orgid = A.Orgid");
        cond.addLeftJoin("TS_UserInfo D on C.HeadUserId = D.UserId  ");
        cond.setOrderby("A.createTime DESC");
        return commonApiService.searchLay(cond);
    }




    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    		) {
        try {
            logger.info("部门账户管理新增传参：" + params);
            //修改
            return tsOrganizationAccountService.add(params);
        } catch (Exception e) {
            logger.error("部门账户管理新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("部门账户管理修改传参：" + params);
            //修改
            return tsOrganizationAccountService.edit(params);
        } catch (Exception e) {
            logger.error("部门账户管理修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

}
